﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
//using System.Diagnostics;

namespace siguo_shrx.client
{
    public class ProcessApi
    {
        //HANDLE OpenProcess(
        //    DWORD dwDesiredAccess,// access flag
        //    BOOL bInheritHandle,	// handle inheritance flag
        //    DWORD dwProcessId 	// process identifier
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);

        //BOOL CloseHandle(
        //    HANDLE hObject 	// handle to object to close
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            Int32 CloseHandle(IntPtr hObject);

        //BOOL WriteProcessMemory(
        //    HANDLE hProcess,	// handle to process whose memory is written to
        //    LPVOID lpBaseAddress,	// address to start writing to
        //    LPVOID lpBuffer,	// pointer to buffer to write data to
        //    DWORD nSize,	// number of bytes to write
        //    LPDWORD lpNumberOfBytesWritten 	// actual number of bytes written
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, uint[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesWritten);

        //BOOL ReadProcessMemory(
        //    HANDLE hProcess,	// handle of the process whose memory is read
        //    LPCVOID lpBaseAddress,	// address to start reading
        //    LPVOID lpBuffer,	// address of buffer to place read data
        //    DWORD nSize,	// number of bytes to read
        //    LPDWORD lpNumberOfBytesRead 	// address of number of bytes read
        //    );
        [DllImport("kernel32.dll")]
        public static extern
            Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, uint[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesRead);

        [DllImport("USER32.DLL")]
        public static extern
            IntPtr FindWindow(string lpClassName, string lpWindowName);

        //获取当前窗口句柄:GetForegroundWindow()
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        //获取窗口标题。
        [DllImport("user32.dll", EntryPoint = "GetWindowText")]
        public static extern int GetWindowText(
        int hwnd,
            //string lpString,
        StringBuilder lpstring,
        int cch
        ); 
    }
}
